home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / DATETIME.SWG / 0014_PACKTIME.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  3KB  |  96 lines

  1. {>I noticed that Pascal has Functions called unpacktime() and packtime().
  2. >Does anyone know how these two Functions work?  I need either a source
  3. >code example of the equiValent or just a plain algorithm to tell me how
  4. >these two Functions encode or Decode and date/time into a LongInt.
  5.  
  6.   The packed time Format is a 32 bit LongInt as follows:
  7.  
  8.    bits     field
  9.    ----     -----
  10.    0-5   =  seconds
  11.    6-11  =  minutes
  12.    12-16 =  hours
  13.    17-21 =  days
  14.    22-25 =  months
  15.    26-31 =  years
  16.  
  17.   DateTime is a Record structure defined Within the Dos Unit With the
  18.   following structure:
  19.  
  20.    DateTime = Record
  21.      year,month,day,hour,min,sec : Word
  22.      end;
  23.  
  24.   The GetFtime Procedure loads the date/time stamp of an opened File
  25.   into a LongInt.  UnPackTime extracts the Various bit patterns into the
  26.   DateTime Record structure.  PackTime will take the Values you Assign
  27.   to the DateTime Record structure and pack them into a LongInt - you
  28.   could then use SetFTime to update the File date stamp.  A small sample
  29.   Program follows.
  30. }
  31. Program prg30320;
  32.  
  33. Uses
  34.   Dos;
  35.  
  36. Var
  37.   TextFile : Text;
  38.   Filetime : LongInt;
  39.   dt : DateTime;
  40.  
  41. begin
  42.   Assign(TextFile,'TextFile.txt');
  43.   ReWrite(TextFile);
  44.   WriteLn(TextFile,'Hi, I''m a Text File');
  45.   GetFtime(TextFile,Filetime);
  46.   Close(TextFile);
  47.   UnPackTime(Filetime,dt);
  48.   WriteLn('File was written: ',dt.month,'/',dt.day,'/',dt.year,
  49.                         ' at ',dt.hour,':',dt.min,':',dt.sec);
  50.   ReadLn;
  51. end.
  52.  
  53. {
  54. The following example shows how to pick apart the packed date/time.
  55. }
  56.  
  57. Program PKTIME;
  58. Uses
  59.   Dos;
  60.  
  61. Var
  62.   dt : DateTime;
  63.   pt : LongInt;
  64.   Year  : 0..127;    { Years sInce 1980 }
  65.   Month : 1..12;     { Month number }
  66.   Day   : 1..31;     { Day of month }
  67.   Hour  : 0..23;     { Hour of day }
  68.   Min   : 0..59;     { Minute of hour }
  69.   Sec2  : 0..29;     { Seconds divided by 2 }
  70.  
  71. Procedure GetDateTime(Var dt : DateTime);
  72. { Get current date and time. Allow For crossing midnight during execution. }
  73. Var
  74.   y, m, d, dow : Word;
  75.   Sec100       : Word;
  76. begin
  77.   GetDate(y, m, d, dow);
  78.   GetTime(dt.Hour, dt.Min, dt.Sec, Sec100);
  79.   GetDate(dt.Year, dt.Month, dt.Day, dow);
  80.   if dt.Day <> d then
  81.     GetTime(dt.Hour, dt.Min, dt.Sec, Sec100);
  82. end;
  83.  
  84. begin
  85.   GetDateTime(dt);
  86.   PackTime(dt, pt);
  87.   Year  := (pt shr 25) and $7F;
  88.   Month := (pt shr 21) and $0F;
  89.   Day   := (pt shr 16) and $1F;
  90.   Hour  := (pt shr 11) and $1F;
  91.   Min   := (pt shr  5) and $3F;
  92.   Sec2  := pt and $1F;
  93.   WriteLn(Month, '/', Day, '/', Year+1980);
  94.   WriteLn(Hour,  ':', Min, ':', Sec2*2);
  95. end.
  96.